package ch.csp.transformer;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import ch.csp.exchange.ParagraphWithWords;

public class File2ParagraphWithWords {

	// Claudio

	final static Charset ENCODING = StandardCharsets.UTF_8;
	private String inputFilePath;

	public File2ParagraphWithWords(String inputFilePath) {
		this.inputFilePath = inputFilePath;
	}

	public ArrayList<ParagraphWithWords> transform() {
		ArrayList<String> listParagraphs = separateParagraphsFromLines(getLinesFromFile());
		ArrayList<ArrayList<String>> list = getWordsFromParagraphsAndRemoveWhiteSpaces(listParagraphs);
		return parseListToParagraphWithWords(list);
	}

	private ArrayList<String> getLinesFromFile() {
		ArrayList<String> returnValue = new ArrayList<String>();
		Path path = Paths.get(this.inputFilePath);
		try (Scanner scanner = new Scanner(path, ENCODING.name())) {
			while (scanner.hasNextLine()) {
				returnValue.add(scanner.nextLine());
			}
			return returnValue;
		} catch (IOException io) {
			io.printStackTrace();
			return null;
		}
	}

	private ArrayList<String> separateParagraphsFromLines(
			ArrayList<String> lines) {
		ArrayList<String> paragraphs = new ArrayList<>();

		Iterator<String> lineIterator = lines.iterator();
		String tmp = "";
		String para = "";
		while (lineIterator.hasNext()) {
			tmp = lineIterator.next();
			if ((tmp = reduceWhitespacesToSingle(tmp)).isEmpty() || tmp.equals(" ")) {
				paragraphs.add(para);
				para = "";
				continue;
			}
			para += tmp;
		}
		return paragraphs;
	}

	private void printList(List<String> listToPrint) {
		Iterator<String> listIterator = listToPrint.iterator();
		while (listIterator.hasNext()) {
			System.out.println(listIterator.next());
		}
	}

	private ArrayList<ArrayList<String>> getWordsFromParagraphsAndRemoveWhiteSpaces(
			ArrayList<String> paragraphs) {
		ArrayList<ArrayList<String>> paragraphsWithWords = new ArrayList<>();
		Iterator<String> paragraphIterator = paragraphs.iterator();
		while (paragraphIterator.hasNext()) {
			paragraphsWithWords
					.add(splitParagraphStringIntoWords(paragraphIterator.next()));
		}
		return paragraphsWithWords;
	}
	
	private String removeWhitespaces(String string){
		return string.replaceAll("\\p{Z}","");
	}
	
	private String reduceWhitespacesToSingle(String string){
		return string.replaceAll("\\p{Z}"," ");
	}

	private ArrayList<String> splitParagraphStringIntoWords(
			String paragraphToSplit) {
		//paragraphToSplit = paragraphToSplit.replaceAll("\\p{Z}", " ");
		String[] splitArray = reduceWhitespacesToSingle(paragraphToSplit).split(" ");
		ArrayList<String> listOfWords = new ArrayList<>();
		for (String s : splitArray) {
			if (!s.isEmpty()) {
				listOfWords.add(removeWhitespaces(s));
			}
		}
		return listOfWords;
	}
	
	private ArrayList<ParagraphWithWords> parseListToParagraphWithWords(ArrayList<ArrayList<String>> paragraphWithWords){
		Iterator<ArrayList<String>> paragraphIterator = paragraphWithWords.iterator();
		ArrayList<ParagraphWithWords> convertedValues = new ArrayList<>();
		while(paragraphIterator.hasNext()){
			ParagraphWithWords tmp = new ParagraphWithWords();
			for(String wordFromParagraph : paragraphIterator.next()){
				tmp.words.add(wordFromParagraph);
			}
			convertedValues.add(tmp);
		}
		return convertedValues;
	}
}
